package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 完善个人信息
     * @param param //Map集合用来存储请求体中的表单信息
     * @param token //String用来获取请求头中的token信息
     * @return
     */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> loginReginfo(@RequestBody Map<String,String> param, @RequestHeader("Authorization") String token){
        //传递参数给service,返回true保存成功，false保存失败
        Boolean flag = userInfoService.loginReginfo(param,token);

        if (flag){
            //成功返回 200
            return ResponseEntity.ok(null);
        }

        //失败将错误信息封装，返回
        ErrorResult errorResult = ErrorResult.builder().errCode("00002").errMessage("用户信息保存失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 用户头像上传
     * @param multipartFile //头像文件
     * @param token //token
     * @return
     */
    @PostMapping("loginReginfo/head")
    public ResponseEntity<Object> saveUserLogo(@RequestParam("headPhoto") MultipartFile multipartFile,
                                               @RequestHeader("Authorization") String token){

        boolean flag = userInfoService.saveUserLogo(multipartFile,token);

        if (flag){
            return ResponseEntity.ok(null);
        }

        //失败将错误信息封装，返回
        ErrorResult errorResult = ErrorResult.builder().errCode("00002").errMessage("用户logo保存失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
